import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.WriteAbortedException;
import java.io.Writer;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.*;

/**
 * @author TA "Cunxin Jia"
 * @author modified by Dr. "Wei Hu"
 */
public class sparql
{
	static Model model = ModelFactory.createDefaultModel();
	
	public static void main(String[] args) throws IOException
	{		
		sparql sample= new sparql();
		model.read("file:new_soccer.owl");
		sample.queryofInstance();
		sample.querywithFilter();
		sample.construct();
		sample.ask();	
	}

	public void queryofInstance() throws IOException
	{
		FileOutputStream fout=new FileOutputStream("queryanswer.txt",true);
		FileWriter fw=new FileWriter("queryanswer.txt");
		String queryString1 = 
			"PREFIX soccer: <http://www.semanticweb.org/ontologies/2012/6/Ontology1342505298961.owl#> " +
			"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
			"SELECT ?forward " +
			"WHERE {?forward rdf:type soccer:Player." +
			"?forward soccer:plays_position soccer:Forward}" ;
	
		Query query1 = QueryFactory.create(queryString1);
		QueryExecution qe1 = QueryExecutionFactory.create(query1, model);
		ResultSet results1 = qe1.execSelect();
		fw.write("queryofInstance:\n");
		fw.close();
		ResultSetFormatter.out(fout, results1, query1);
	
		// Remember to free the used resources during running the query
		qe1.close();
		fout.close();
	}
	
	public void querywithFilter() throws IOException 
	{
		FileOutputStream fout=new FileOutputStream("queryanswer.txt",true);
		FileWriter fw=new FileWriter("queryanswer.txt",true);
		String queryString1 = 
			"PREFIX soccer: <http://www.semanticweb.org/ontologies/2012/6/Ontology1342505298961.owl#> " +
			"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
			//"SELECT ?s ?p ?o " +
			//"WHERE {?s ?p ?o}";
			"SELECT ?PlayerfromEngland " +
			"WHERE {?PlayerfromEngland rdf:type soccer:Player." +
			"?PlayerfromEngland soccer:nationality ?n " +
			"  FILTER(?n = \"England\")}";
	
		Query query1 = QueryFactory.create(queryString1);
		QueryExecution qe1 = QueryExecutionFactory.create(query1, model);
		ResultSet results1 = qe1.execSelect();
		fw.write("querywithFilter\n");	
		fw.close();
		ResultSetFormatter.out(fout, results1, query1);
	
		// Remember to free the used resources during running the query
		qe1.close();
		fout.close();
	}
	
	public void construct() throws IOException 
	{
		FileOutputStream fout=new FileOutputStream("queryanswer.txt",true);
		FileWriter fw=new FileWriter("queryanswer.txt",true);
		String queryString1 = 
			"PREFIX soccer: <http://www.semanticweb.org/ontologies/2012/6/Ontology1342505298961.owl#> " +
			"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
			"CONSTRUCT {?p soccer:position ?age}" +
			"WHERE {?p soccer:plays_position ?age}" ;
	
		Query query1 = QueryFactory.create(queryString1);
		QueryExecution qe1 = QueryExecutionFactory.create(query1, model);
		Model result3 = qe1.execConstruct();
		fw.write("construct:\n");
		fw.close();
		result3.write(fout);
	
		// Remember to free the used resources during running the query
		qe1.close();		
		fout.close();
	}
	
	public void ask() throws IOException
	{
		FileWriter fw=new FileWriter("queryanswer.txt",true);
		String queryString1 = 
			"PREFIX soccer: <http://www.semanticweb.org/ontologies/2012/6/Ontology1342505298961.owl#> " +
			"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
			"ASK {?player soccer:isemployeeof ?team " +
			" FILTER(?team = \"Barcelona\")}";
	
		Query query1 = QueryFactory.create(queryString1);
		QueryExecution qe1 = QueryExecutionFactory.create(query1, model);
		Boolean result4 = qe1.execAsk();
		fw.write("ask:\n");
		if (result4)
			fw.write("Yes\n");	
		else
			fw.write("No\n");
		// Remember to free the used resources during running the query
		qe1.close();
		fw.close();
	}
	
}


